package com.swinginwind.flexutils.components
{
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.collections.ArrayList;
	import mx.collections.IList;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.core.mx_internal;
	import mx.events.CollectionEvent;
	import mx.events.ItemClickEvent;
	
	import spark.components.ComboBox;
	import spark.components.DataGroup;
	import spark.events.DropDownEvent;
	
	use namespace mx_internal;
	
	[Event(name="addItem", type="flash.events.Event")]
	[Event(name="removeItem", type="flash.events.Event")]
	
	/**
	 * @author jimmy
	 * @E-mail: swinginwind@gmail.com
	 * @version 1.0.0
	 * 创建时间：2012-12-15 上午3:22:46
	 * 
	 */
	
	public class ComboCheck extends ComboBox
	{
		
		private var _selectedItems:IList;
		
		public function ComboCheck()
		{
			super();
			setStyle("skinClass", ComboCheckSkin);
			addEventListener(ItemClickEvent.ITEM_CLICK, onItemClick);
		}
		
		
		
		override public function set dataProvider(value:IList):void {
			super.dataProvider = value;
			
			// Load initial selected items
			selectedItems = new Vector.<Object>;
			
			for each (var item:Object in dataProvider) {
				if (item.selected) {
					_selectedItems.addItem(item);
				}
			}
		}
		
		[Bindable("change")]
		[Bindable("valueCommit")]
		[Bindable("collectionChange")]
		public function set selectedItemList(value:IList):void {
			_selectedItems = value;
		}
		
		public function get selectedItemList():IList {
			return _selectedItems;
		}
		
		override protected function keyDownHandler(event:KeyboardEvent):void {
			super.keyDownHandler(event);
			if (event.keyCode == Keyboard.ENTER && isDropDownOpen) {
				
				var currentItem:Object = getItem(textInput.text);
				
				if (currentItem != null) {
					currentItem.selected = !currentItem.selected;
					var e:ItemClickEvent = new ItemClickEvent(ItemClickEvent.ITEM_CLICK, true);
					e.item = currentItem;
					dispatchEvent(e);
					
					// Update items
					dataGroup.dataProvider.itemUpdated(currentItem,null,currentItem,e.item);
				}
				
			}
			trace(selectedItem);
		}
		
		override protected function capture_keyDownHandler(event:KeyboardEvent):void {
			if (event.keyCode == Keyboard.ENTER) {
				return;
			}
			super.capture_keyDownHandler(event);
		}
		
		private function getItem(text:String):Object {
			for each (var item:Object in dataProvider) {
				if (item[labelField] == text)
					return item;
			}
			
			return null;
		}
		
		override protected function item_mouseDownHandler(event:MouseEvent):void {
			trace (event);
		}
		
		public function itemClick(item:Object, selected:Boolean):void
		{
			if(!_selectedItems)
				_selectedItems = new ArrayList();
			if(selected)
			{
				_selectedItems.addItem(item);
				dispatchEvent(new Event("addItem"));
			}
			else
			{
				var index:int = _selectedItems.getItemIndex(item);
				if(index != -1)
					_selectedItems.removeItemAt(index);
				dispatchEvent(new Event("removeItem"));
			}
			
		}
		
		private function onItemClick(event:ItemClickEvent):void {
			trace("item checked!");
			if(!_selectedItems)
				_selectedItems = new ArrayList();
			if (event.item.selected)  {
				_selectedItems.addItem(event.item);
				dispatchEvent(new Event("addItem"));
			} else {
				var index:int = _selectedItems.getItemIndex(event.item);
				if(index != -1)
					_selectedItems.removeItemAt(index);
				dispatchEvent(new Event("removeItem"));
			}
			
			dispatchEvent(new Event("valueCommit"));
		}
	}
}